
dojo.require("simp.DragResizeRegion");

dojo.provide("ResizeEditor");
dojo.declare("ResizeEditor", BaseEditor, {
	imageId: "image2Resize",
	imageResizeId: "imageRegion",
	image: {},
	resizer: {},
	preset: {},
	_editorEvent: null,
	_presetEvent: null,
	_commitEvent: null,
	_resizeEvent: null,
	
	constructor: function() {
		this._moduleName = "resize";
		//this.imageResizeId = imageRegion;
		//this.imageId = img;
		ctrl.registerEditor(this)
		ctrl.getEditor(this._moduleName, this.setEditor);
	},

	setEditor: function(result) {
		console.debug("resize editor markup loaded");
		ctrl.editors.item("resize").editorMarkup = result.editor;
	},
	
	getEditorMarkup: function(result) {
		return this.editorMarkup
	},
	
	activate: function(image) {
		this.deactivate();
		console.debug("Resizer activation");
		this.resizer = dijit.byId(this.imageResizeId);
		console.debug("Resizer: resize is - " + dojo.debugReturn(this.resizer));
		this.image = dojo.byId(this.imageId);
		if (image != null) {
			this.image.src = image.url;
			dojo.style(this.image, "height", image.height + "px");
			dojo.style(this.image, "width", image.width + "px");
			dojo.style(this.resizer.domNode, "height", image.height + "px");
			dojo.style(this.resizer.domNode, "width", image.width + "px");
		} 
		console.debug("Resizer: image is - " + dojo.debugReturn(this.image));	
		console.debug("Resizer: Widgets references initalized");
		this._commitEvent = dojo.connect(this.resizer, "onRegionDblClick", this, "_onCommit");
		this._resizeEvent = dojo.connect(this.resizer, "onResize", this, "_onResize");	
		console.debug("Resizer: getCoords - redefine coords");
		var oldCoords = this.resizer.getCoords();
		console.debug("Resizer: get coords of the image");
		var imageCoords = dojo.coords(this.image);
		console.debug("Resizer: define new coords");
		var coords = {
			l: oldCoords.l,
			t: oldCoords.t,
			h: imageCoords.h,
			w: imageCoords.w,
		};
		console.debug("Resizer: set new Coords");
		this.resizer.setCoords(coords);
		console.debug("Resizer: getBounds - redefine bounds");
		bounds = this.resizer.getBounds();
		bounds.h = 3000;
		bounds.w = 5000;
		this.resizer.setBounds(bounds);
		console.debug("Resizer activated");
		//this.resizer.bounds.h = 3000;
		//this.resizer.bounds.w = 5000;
	},
	
	deactivate: function() {
		dojo.disconnect(this._commitEvent);
		dojo.disconnect(this._resizeEvent);
	},
	
	setValues: function(values) {
		if (this.resizer != null && values != null) {
			this.resizer.min = values.min;
			this.resizer.max = values.max;
			this.resizer.holdAspectRatio = values.hold_aspect;
			this.resizer.setCoords(values.coords);
		}
	},
	
	editorLoaded: function() {
		if (ctrl.module = "resize") {
			this.resizer = dijit.byId(this.imageResizeId);
			this._commitEvent = dojo.connect(this.resizer, "onRegionDblClick", this, "onCommit")
			this._resizeEvent = dojo.connect(this.resizer, "onResize", this, "onResize");
			if (djConfig.isDebug)
				console.debug("editor loaded")
			//dojo.disconnect(this._commitEvent);
		} else {
			dojo.disconnect(this._editorEvent);
			dojo.disconnect(this._presetEvent);
			dojo.disconnect(this._commitEvent);
			dojo.disconnect(this._resizeEvent);
			this.resizer.destroy();
		}
	},
	
	valuesLoaded: function() {
		if (ctrl.module == "resize") {
			this.resizer.min = ctrl.values.min
			this.resizer.max = ctrl.values.max
			this.resizer.holdAspect = ctrl.values.holdAspect
			this.resizer.setCoords(ctrl.values.coords)
			//this.resizer.sizeable = ctrl.values.sizeable
			//this.resizer.setup()
		}
	},		
	
	_onCommit: function() {
		coords = this.getValues();
		values = {w: coords.w, h: coords.h}		
		ctrl.commit(values)
	},
	
	_onResize: function() {
		//if (djConfig.isDebug)
		//	console.debug("Editor - onResize: " + dojo.debugReturn(this.resizer.getCoords()));
		domImg = dojo.byId(this.imageId);
		console.debug("got domImg")
		coords = this.resizer.getCoords();
		console.debug("got resizer coords");
		dojo.style(domImg, "width", coords.w + "px");
		dojo.style(domImg, "height", coords.h + "px");
		if (djConfig.isDebug)
			console.debug("Editor - Image Resized to width: " + dojo.style(domImg, "width") + ", height: " + dojo.style(domImg, "height"));		
	},
	
	getValues: function() {
		return this.resizer.getCoords()
	},
	
	
});

dojo.addOnLoad( function () {
	//dojo.parser.parse();
	resizer = new ResizeEditor();
});